1
Beyond Individual Variables
AI017 Lesson 6
00:00

While basic collections like arrays or slices are limited to storing elements of the same data type, structures (structs) allow us to group disparate types into a single unit. This is essential for Martian exploration, where telemetry involves mixed data like names (string) and coordinates (float64).

1. Semantic Grouping vs. Uniformity

Unlike a slice of floats which only tells you "how many," a struct provides a labeled container. Definition: Whereas collections are of the same type, structures allow you to group disparate things together. This prevents "variable soup" by bundling related data into named objects.

a, b = b, a // Efficient state swapping

2. Atomicity in Operations

By grouping variables, we achieve atomicity. Functions like func Step(a, b Universe) or func (u Universe) Next(x, y int) bool can manipulate entire environments rather than chasing loose primitives. For one-off tasks, anonymous structures (like in Listing 21.1) provide immediate organization without formal type definitions.

Variable Souplat1, lat2long1, long2name1, name2Structured Roverstruct { lat, long, name }Single Atomic Unit
main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>